package com.example.wechat.csdnandroid.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.util.Log;

public class VolumeService extends Service {
    private final String CONNECTIVITY_ACTION = ConnectivityManager.CONNECTIVITY_ACTION;
    private static final String TAG = "VolumeService";
    private BroadcastReceiver mReciver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //这里处理监听到的消息
            String action = intent.getAction();
            if (CONNECTIVITY_ACTION.equals(action)) {
                if (connectivityManager == null) {
                    connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                }
                if (connectivityManager == null) {
                    return;
                }
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                if (activeNetworkInfo != null && activeNetworkInfo.isAvailable()) {
                    Log.e(TAG, "onReceive:当前网络状态: " + activeNetworkInfo.getTypeName());
                    //存储到一个全局变量中
                } else {
                    Log.e(TAG, "onReceive:没有网络、 ");

                }

            }

        }
    };
    private ConnectivityManager connectivityManager;

    public VolumeService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //注册广播 并开启


        registerReceiver(mReciver, new IntentFilter(CONNECTIVITY_ACTION));


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mReciver != null) {
            unregisterReceiver(mReciver);
        }
    }
}
